//
//  UIColor+Expanded.h
//  ChartsDemo
//
//  Created by Kary on 2022/10/2.
//

#import "UIColor+Expanded.h"

@implementation UIColor (Expanded)

+ (UIColor *)randomColor {
    return [UIColor colorWithRed:(arc4random()%256)/256.f
                           green:(arc4random()%256)/256.f
                            blue:(arc4random()%256)/256.f
                           alpha:1.0f];
}


+ (UIColor *)colorFromARGB:(NSString *)hexString {
    if (!hexString || hexString.length == 0 || [hexString isEqualToString:@"#"]) {
        return [UIColor clearColor];
    }
    NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    if([cleanString length] == 3) {
        cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
                       [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
                       [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
                       [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
    }else if([cleanString length] == 4){
        cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@",
                       [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
                       [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
                       [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)],
                       [cleanString substringWithRange:NSMakeRange(3, 1)],[cleanString substringWithRange:NSMakeRange(3, 1)]];
    }else if([cleanString length] == 6) {
        cleanString = [NSString stringWithFormat:@"%@%@", @"ff",cleanString];
    }else{
        return [UIColor clearColor];
    }
    
    NSString *aString = [cleanString substringWithRange:NSMakeRange(0, 2)];
    NSString *rString = [cleanString substringWithRange:NSMakeRange(2, 2)];
    NSString *gString = [cleanString substringWithRange:NSMakeRange(4, 2)];
    NSString *bString = [cleanString substringWithRange:NSMakeRange(6, 2)];
    
    // Scan values(16进制转10进制)
    unsigned int a, r, g, b;
    [[NSScanner scannerWithString:aString] scanHexInt:&a];
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:r/255.f green:g/255.f blue:b/255.f alpha:a/255.f];
    
}

+ (UIColor *)colorFromARGB:(NSString *)hexString
                         alpha:(CGFloat)alpha {
    UIColor *color = [[UIColor colorFromARGB:hexString] colorWithAlphaComponent:alpha];
    return color;
}


+ (instancetype)colorGradientChangeWithSize:(CGSize)size
                                     direction:(IHGradientChangeDirection)direction
                                    startColor:(UIColor *)startcolor
                                      endColor:(UIColor *)endColor {
    
    if (CGSizeEqualToSize(size, CGSizeZero) || !startcolor || !endColor) {
        return nil;
    }
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);

    CGPoint startPoint = CGPointZero;
    if (direction == IHGradientChangeDirectionDownDiagonalLine) {
        startPoint = CGPointMake(0.0, 1.0);
    }
    if (direction == IHGradientChangeDirectionHorizontal) {
        startPoint = CGPointMake(0.0, 0.5);
    }
    gradientLayer.startPoint = startPoint;
    
    CGPoint endPoint = CGPointZero;
    switch (direction) {
        case IHGradientChangeDirectionLevel:
            endPoint = CGPointMake(1.0, 0.0);
            break;
        case IHGradientChangeDirectionVertical:
            endPoint = CGPointMake(0.0, 1.0);
            break;
        case IHGradientChangeDirectionUpwardDiagonalLine:
            endPoint = CGPointMake(1.0, 1.0);
            break;
        case IHGradientChangeDirectionDownDiagonalLine:
            endPoint = CGPointMake(1.0, 0.0);
            break;
        case IHGradientChangeDirectionHorizontal:
            endPoint = CGPointMake(1, 0.5);
            break;
        default:
            break;
    }
    gradientLayer.endPoint = endPoint;
    gradientLayer.colors = @[(__bridge id)startcolor.CGColor, (__bridge id)endColor.CGColor];
    UIGraphicsBeginImageContext(size);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return [UIColor colorWithPatternImage:image];
}
@end
